//
//  SportViewController.m
//  Lightthings
//
//  Created by Hero on 2024/11/15.
//

#import "SportViewController.h"
#import "SportModeModel.h"
#import "SportTopView.h"
#import "SportDetailModel.h"

@interface SportViewController ()

/* <#note#> */
@property (nonatomic, strong) SportTopView * sportTopView;
@property (nonatomic, strong) SportModeModel * walkModel, *runModel, *bikeModel, *mountModel, *yogaModel, *swimModel,  *weightliftingModel, *ropeSkippingModel;
@property (nonatomic, assign) unsigned long stepData;

@end

@implementation SportViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self readData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _sportTopView = [[SportTopView alloc] initWithFrame:CGRectMake(15, klSafeAreaEdgeInsets.top, kScreenWidth - 30, 120)];
    [_sportTopView updateCurrentStep:0 CurrentHot:0 Target:0 ];
    [self.view addSubview:_sportTopView];
    
    [JYInitializer jy_initImageViewWithImage:U_imageNamed(@"指示箭头") on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(15); kMakeWHV(20, 20);
        kMakeT(self.sportTopView.mas_bottom).offset(15);
    }];
    
    [JYInitializer jy_initLabelWithText:SWString(@"运动模式") textColor:KLColor(@"666666") font:U_sfont(14) bgColor:[UIColor clearColor] on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(15+20+ 5);
        kMakeT(self.sportTopView.mas_bottom).offset(15);
        kMakeHV(20);
    }];
    

    self.collectionInsets = kEdge(klSafeAreaEdgeInsets.top + _sportTopView.height + 50, 0, 0, 0);
    
    MainSectionModel * s = [[MainSectionModel alloc] init];
    [s sectionInsets:kEdge(10, 15, 10, 15) ls:10 is:10];

    
    _walkModel = [self cModelWithTitle:SWString(@"健走") Icon:@"健走" textColor:@"#00D9A8"];
    _walkModel.type = SportType_Walking;
    [s.items addObject:_walkModel];
    
    _runModel = [self cModelWithTitle:SWString(@"跑步") Icon:@"跑步" textColor:@"#FF702B"];
    _runModel.type = SportType_Running;
    [s.items addObject:_runModel];
    
    _bikeModel = [self cModelWithTitle:SWString(@"骑行") Icon:@"骑行" textColor:@"#1772BF"];
    _bikeModel.type = SportType_Riding;
    [s.items addObject:_bikeModel];
      
    _bikeModel = [self cModelWithTitle:SWString(@"登山") Icon:@"登山" textColor:@"#0BB953"];
    _bikeModel.type = SportType_Climbing;
    [s.items addObject:_bikeModel];
    
    _yogaModel = [self cModelWithTitle:SWString(@"瑜伽") Icon:@"瑜伽" textColor:@"#FFBB45"];
    _yogaModel.type = SportType_Yoga;
    [s.items addObject:_yogaModel];
    
    _swimModel = [self cModelWithTitle:SWString(@"游泳") Icon:@"游泳" textColor:@"#2BC6FF"];
    _swimModel.type = SportType_Swimming;
    [s.items addObject:_swimModel];
    
    _weightliftingModel = [self cModelWithTitle:SWString(@"举重") Icon:@"举重" textColor:@"#5C46FF"];
    _weightliftingModel.type = SportType_Weightlifting;
    [s.items addObject:_weightliftingModel];
      
    _ropeSkippingModel = [self cModelWithTitle:SWString(@"跳绳") Icon:@"跳绳" textColor:@"#FF71E3"];
    _ropeSkippingModel.type = SportType_RopeSkipping;
    [s.items addObject:_ropeSkippingModel];
    
    self.dataSources = @[s];
    // Do any additional setup after loading the view.
}

- (SportModeModel *)cModelWithTitle:(NSString *)title Icon:(NSString *)icon textColor:(NSString *)tcolor {
    SportModeModel * m = [[SportModeModel alloc] init];
    m.title = title;
    m.icon = icon; m.titleColor = tcolor;
    m.distance = m.step = m.heart = m.hot = @"0";
    return m;
}

- (void)readData {
    if ([FRQBluetoothManager shareManager].curPeripherals.firstObject) {
        CBPeripheral * per = [FRQBluetoothManager shareManager].curPeripherals.firstObject;
        if (per.deviceType.intValue != BleDevice_Watch) {
            return;
        }
        [[WatchCommandTool sharedInstance] readRealTimeDataFromPeripheral:[FRQBluetoothManager shareManager].curPeripherals.firstObject Block:^(BOOL success, unsigned long stepData, unsigned long caloriesData, unsigned long distanceData, unsigned long heartRateData, unsigned long electricData) {
            self.stepData = stepData;
            [self.sportTopView updateCurrentStep:stepData CurrentHot:caloriesData / 1000.0 Target:[FRQBluetoothManager shareManager].curPeripherals.firstObject.targetStep.longLongValue];
            
            
            [[WatchCommandTool sharedInstance] readSportData:^(BOOL success, NSArray<NSArray *> * _Nonnull allData) {
                if (success) {
                    for (int i = 0; i < allData.count; i ++) {
                        SportDetailModel * model = [SportDetailModel creatModelWithInfo:allData[i]];
                        for (SportModeModel * m in self.dataSources.firstObject.items) {
                            if (model.type == m.type) {
                                m.step = model.stepValue;
                                m.distance = model.distanceValue;
                                m.heart = model.rateValue;
                                m.hot = model.caloValue;
                            }
                        }
                        
                    }
                    [self reloadIfNeeded];
                }
            } ToPeripheral:[FRQBluetoothManager shareManager].curPeripherals.firstObject];
        }];
        
    }else {
         
    }

}



- (BOOL)can_pulldown {
    return NO;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
